package com.thz.system.controller.blog;

import com.thz.common.core.controller.BaseController;
import com.thz.common.core.result.Result;
import com.thz.system.pojo.blog.Blog;
import com.thz.system.pojo.blog.dto.AddBlogDTO;
import com.thz.system.pojo.blog.dto.UpdateBlogDTO;
import com.thz.system.pojo.blog.vo.BlogDetailsVO;
import com.thz.system.service.blog.IBlogService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @Author thz
 * @Date 2025/5/10
 */
@Slf4j
@RestController
@RequestMapping("/blog")
@Tag(name = "文章接口")
public class BlogController extends BaseController {

    @Autowired
    private IBlogService blogService;

    @GetMapping("/list")
    public Result<List<Blog>> list(@NonNull @RequestParam("courseId") Long courseId){
        return Result.ok(blogService.list(courseId));
    }

    @PostMapping("/addBlog")
    public Result addBlog(@Validated @RequestBody AddBlogDTO addBlogDTO){
        log.info("添加博客, 标题:{}", addBlogDTO.getTitle());
        return IToResult(blogService.addBlog(addBlogDTO));
    }

    @PostMapping("/updateBlog")
    public Result updateBlog(@Validated @RequestBody UpdateBlogDTO updateBlogDTO){
        log.info("更新博客, 博客ID: {}", updateBlogDTO.getBlogId());
        return UToResult(blogService.updateBlog(updateBlogDTO));
    }

    @PostMapping("/delete")
    public Result deleteBlog(@NotNull @RequestParam("blogId") Long blogId){
        log.info("删除博客, 博客ID: {}", blogId);
        return DToResult(blogService.deleteBlog(blogId));
    }

    @GetMapping("/getBlogDetail")
    public Result<BlogDetailsVO> getBlogDetail(@NotNull @RequestParam("blogId") Long blogId){
        log.info("获取博客详情{}", blogId);
        return Result.ok(blogService.getBlogDetail(blogId));
    }

}
